<?php
namespace app\controller\applets;


/**
 * 控制器基础类
 */
class BaseController{
    
    private $success  = true;
    private $code  = 0;
    private $msg  = '成功';
    private $data = [];
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(){

    }


    public function is_params($params) {
        if(is_array($params)){
            $flag = true;
            if (array_key_exists('success', $params)) {
                $this->success = $params['success'];
                $flag       = false;
            }
            if (array_key_exists('code', $params)) {
                $this->code = $params['code'];
                $flag       = false;
            }
            if (array_key_exists('msg', $params)) {
                $this->msg = $params['msg'];
                $flag       = false;
            }
            if (array_key_exists('data', $params)) {
                $this->data = $params['data'];
            }else{
                if ($flag) {
                    $this->data = $params;
                }
            }
        }else{
            $this->data = $params;
        }
        return [
            'success' => $this->success,
            'code' => $this->code,
            'msg' => $this->msg,
            'data' => $this->data
        ];
    }

    public function return_msg($data=[]){
        $result=$this->is_params($data);
        return json($result);
    }

    public function return_err($data=[]){
        $this->success=false;
        $this->code=1;
        $this->msg='错误';
        $result=$this->is_params($data);
        return json($result);
    }
    //文件下载
    public function down($file){

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            //下载后删除文件
            @unlink($file);
            exit;
        }
    }
    
    /**
     * 给二维数组中的每个一维数组增加一个新的字段
     * @param [type] $arrs    数组
     * @param [type] $filed   新字段
     * @param [type] $filedValue 新字段值
     */
    public function addField($arrs,$filed,$filedValue){
        $arr = array();//一个空数组，用来存储增加了字段一维数组
        foreach ($arrs as $k=>$value) {
            $value[$filed] = $filedValue;
            $arr[]=$value;
        }
        return $arr;
    }

    //md5加密
    public function encrypt($password){
        return md5(md5($password));
    }

    public function generateUuid() {
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            // 32 bits for "time_low"
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
            // 16 bits for "time_mid"
            mt_rand(0, 0xffff),
            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 4
            mt_rand(0, 0x0fff) | 0x4000,
            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            mt_rand(0, 0x3fff) | 0x8000,
            // 48 bits for "node"
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
        );
    }
}
